<template>
  <div>
    <div class="search" style = "height:400px;">
<!--      <el-input placeholder="请输入账号查询" style="width: 200px" v-model="username"></el-input>

      <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>-->
        <el-form :model="form" label-width="100px" style="margin-top: 50px" :rules="rules" ref="formRef">
          <el-form-item prop="LevelOneName" label="一级机构" >
            <el-select v-model="levelOneName" placeholder="请选择一级机构"  >
              <el-option v-for="item in departmentData " :label="item.name" :value="item.id "  :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="LevelTwoName" label="二级机构" >
            <el-select v-model="levelTwoName" placeholder="请选择二级机构"  >
              <el-option v-for="item in levelTwo " :label="item.name" :value="item.id" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="LevelOneName" label="三级机构" >
            <el-select v-model="form.levelThreeId" placeholder="请选择三级机构" @change=" handleLevelThreeChange(form.levelThreeId)" :disabled="isEditing" >
              <el-option v-for="item in levelThree " :label="item.name" :value="item.id" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="positionTypeName" label="职位类型" >
            <el-select v-model="form.positionTypeId" placeholder="请选择职位类型" @change="loadPositions" >
              <el-option v-for="item in  positionTypeNameData " :label="item.name" :value="item.id " :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="positionId" label="职位名称">
            <el-select v-model="form.positionNameId" placeholder="请选择职位名称">
              <el-option v-for="item in  positionNameData " :label="item.name" :value="item.id " :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
<!--        <div slot="footer" class="dialog-footer">
          <el-button @click="fromVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>-->
<!--      <el-input placeholder="请输入账号查询" style="width: 200px" v-model="username"></el-input>-->
      <el-button type="info" plain style="margin-left: 250px" @click="load(1) ">查询</el-button>


    </div>

    <div class="table">
      <el-table :data="tableData" strip @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="序号" width="70" align="center" sortable></el-table-column>
        <el-table-column label="头像">
          <template v-slot="scope">
            <div style="display: flex; align-items: center">
              <el-image style="width: 40px; height: 40px; border-radius: 50%" v-if="scope.row.avatar"
                        :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]"></el-image>
            </div>
          </template>
        </el-table-column>
<!--        <el-table-column prop="username" label="账号"></el-table-column>-->
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="gender" label="性别"></el-table-column>
<!--        <el-table-column prop="phone" label="电话"></el-table-column>-->
<!--        <el-table-column prop="email" label="邮箱" ></el-table-column>-->
<!--        <el-table-column prop="role" label="角色"></el-table-column>-->
        <el-table-column prop="level" label="身份"></el-table-column>
        <el-table-column prop="levelThreeName" label="部门"></el-table-column>
        <el-table-column prop="positionTypeName" label="职位类型"></el-table-column>
        <el-table-column prop="positionName" label="职位名称"></el-table-column>
        <!--  操作/包括删除 编辑      -->
        <el-table-column label="操作" align="center" width="250">
          <template v-slot="scope">
            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="primary" plain @click="display(scope.row)">查看</el-button>
            <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>

          </template>
        </el-table-column>
      </el-table>
      <!--          设置分页-->
      <div class="pagination">
        <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
      </div>
    </div>



  </div>
</template>

<script>
import department from "@/views/manager/Department.vue";

export default {
  //在视频讲解中，这里定义为User，我把它改成Employee
  name: "Employee",
  data() {
    return {
      tableData: [],  // 所有的数据
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      username: null,
      fromVisible: false,
      Vision:false,//查看弹窗
      form: {},
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
        ]
      },
      ids: [],
      departmentData:[],
      levelTwo:[],
      levelThree:[],
      positionTypeNameData:[],
      positionNameData:[],
      levelOneName:null,
      levelTwoName:null,
      levelThreeId:null,
      positionTypeId:null,
      positionId:null,


    }
  },
  created() {
    // this.load(1)
    this.loadLevelOne()
    this.loadlevelTwo()
    this.loadlevelThree()
    this.loadPositionTypeName()
    this.loadPositionNameData()
    // this.loadPositions()
    /*this.Test()
    this. allConditionsFilled()*/
    /*this.loadPositionTypeName()*/

  },
  methods: {

    /*查询职位类型信息 接口调用成功*/
    loadPositionTypeName(){
      this.$request.get('/positionTypes/selectAll').then(res => {
        if (res.code === '200') {
          console.log(res)
          this.positionTypeNameData= res.data
        } else {
          console.log(res.msgs)
          this.$message.error(res.msg)
        }
      })
    },
    loadPositionNameData(){
      this.$request.get('/Positions/selectAll').then(res => {
        if (res.code === '200') {
          console.log(res)
          this.positionNameData= res.data
        } else {

          this.$message.error(res.msg)
        }
      })
    },
    /*查询一级机构的信息*/
    loadLevelOne(){
      this.$request.get('/levelOne/selectAll').then(res => {
        if (res.code === '200') {
          // console.log(res)
          this.departmentData = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 根据职位类型加载职位名称
    loadPositions() {
      console.log("调用了该方法")
      const positionTypeId = this.form.positionTypeId;
      console.log("我是ID"+positionTypeId)

      // 清空职位名称数据
      this.positionNameData = [];

      // 等待 Vue 更新 DOM 后，再发送请求
      this.$nextTick(() => {
        // 发送请求获取该职位类型下的职位列表
        this.$request.get(`/Positions/names/${positionTypeId}`).then(res => {
          if (res.code === '200') {
            console.log("我调用啦啦啦啦啦啦")
            this.positionNameData = res.data;  // 更新职位名称数据
            console.log(res)
          } else {
            this.$message.error(res.msg);
          }
        }).catch(error => {
          this.$message.error('获取职位名称数据失败');
          console.error(error);
        });
      });
    },
    /*查询二级机构信息*/
    loadlevelTwo(){
      this.$request.get('/levelTwo/selectAll').then(res => {
        if (res.code === '200') {
          this.levelTwo = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    loadlevelThree(){
      this.$request.get('/levelThree/selectAll').then(res => {
        if (res.code === '200') {
          this.levelThree = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleAdd() {   // 新增数据
      this.form = {}  // 新增数据的时候清空数据
      this.fromVisible = true   // 打开弹窗
    },
    handleEdit(row) {   // 编辑数据
      console.log("-----------------")
      console.log(row)
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.isEditing = true;  // 设置为编辑状态
      this.fromVisible = true   // 打开弹窗
    },
    // 判断字段是否被禁用
    isFieldDisabled(field) {
      return this.disabledFields.includes(field);
    },
    display(row){
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.Vision = true
    },

    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)
    },
// 检查是否所有条件都已填写
    allConditionsFilled() {
      return this.form.levelThreeId  &&
      this.form.positionTypeId &&
      this.form.positionNameId ;
},
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum;
      // 检查是否所有条件都已填写
      if (!this.allConditionsFilled()) {
        this.$message.warning('请填写所有查询条件');
        return;
      }
      this.$request.get('/employee/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          levelThreeId: this.form.levelThreeId,
          positionTypeId: this.form.positionTypeId,
          positionNameId: this.form.positionNameId
        }
      }).then(res => {
        console.log("+++++++++++++++++++++++++++++++++++++")
        // console.log(res)
        this.tableData = res.data?.list;
        this.total = res.data?.total;
      });
    },

    reset() {
      this.username = null
      this.load(1)
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },

  }
}
</script>

<style scoped>

</style>